import { createApp } from 'vue'
import router from './routers'
import App from './App.vue'
// 1. 引入你需要的组件
import Vant from 'vant';
// 2. 引入组件样式
import 'vant/lib/index.css';

createApp(App)
    .use(router)
    .use(Vant)
    .mount('#app')
router.beforeEach((to, from, next) => {
    let isLogin = to.meta.isLogin ?? false;
    let isNotLogin = to.meta.isNotLogin ?? false;
    let { cookies } = useCookies();
    if (isLogin && !isNotLogin) {
        let LoginUser = cookies.get('LoginUser')
        if (!LoginUser) {
            showSuccessToast('请先登录账号')
            setTimeout(() => {
                next('/business/login')
            }, 1000);
        } else {
            let data = {
                'id': LoginUser.hasOwnProperty('id') ? LoginUser['id'] : 0,
                'mobile': LoginUser.hasOwnProperty('mobile') ? LoginUser['mobile'] : ''
            }
            Api.business.checklogin(data).then(result => {
                if (result.code === 0) {
                    showSuccessToast('非法登录账号')
                    cookies.remove('LoginUser')
                    setTimeout(() => {
                        next('/business/login')
                    }, 1000);
                } else {
                    let time = new Date(new Date().getTime() + 12 * 60 * 60 * 1000);
                    cookies.set('LoginUser', result.data, time);
                    next()
                }
            })


        }

    } else {
        next()
    }
})
